﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using Passwords.Biz;
using Passwords.Utils;
using Silvermoon.UI;

namespace Passwords.Pages
{
    public class LengthPicker : Page
    {
        private List<int> values;
        private PasswordItem item;
        private ListView listView;
        private PasswordType type;

        private ListViewController<TextItem, int> controller;
        public LengthPicker(PasswordType type)
            : base()
        {
            this.type = type;
            SetTransition(250, TransitionMask.Fade | TransitionMask.Explode, TransitionMask.Fade | TransitionMask.Explode);
            Title = "CHOOSE PASSWORD LENGTH";
            listView = new ListView { Margin = Margin.Zero };
            Controls.Add(listView);
            controller = new ListViewController<TextItem, int>(listView) { ItemHeight = 80 };
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<TextItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<TextItem>>(controller_InitializeItem);
            controller.ItemStyle = StylePool.PickerItemStyle;
            values = new List<int>(38);
            for (int i = 3; i <= 24; i++) values.Add(i);
            controller.DataSource = values;
        }

        public PasswordItem Item
        {
            get { return item; }
            set
            {
                item = value;
                OnItemChanged(item);
            }
        }

        private void OnItemChanged(PasswordItem item)
        {
            int index = values.IndexOf(item.Value.Length);
            listView.SelectedItemIndex = index;
            index = Math.Min(0, index - 1);

        }

        protected override void OnShowing()
        {
            base.OnShowing();
            Schedule(BringSelectedItemIntoView);
        }


        private void BringSelectedItemIntoView()
        {
            int index = Math.Min(listView.Count - 1, listView.SelectedItemIndex + 3);
            if (index >= 0) listView.BringItemIntoView(index, true);
        }

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<TextItem> e)
        {
            //e.Item.Margin = new Margin(10, 0, 10, 10);
            e.Item.Alignment = Alignment.Center;
            e.Item.Tap += new EventHandler<TapEventArgs>(Item_Tap);
        }

        void Item_Tap(object sender, TapEventArgs e)
        {
            Item item = sender as Item;
            int len = (int)item.DataValue;
            if (this.Item != null)
            {
                Item.Value = PasswordUtility.GeneratePassword(type, len);
                GoBack();
            }
        }


        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<TextItem> e)
        {
            e.Item.Text = ((int)e.Value).ToString();
        }

    }
}
